package proxy.wrappers;

import android.os.IInterface;
import android.view.IRotationWatcher;
import com.genymobile.scrcpy.Ln;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/* loaded from: classes2.dex */
public final class WindowManager {
    private Method freezeRotationMethod;
    private Method getRotationMethod;
    private Method isRotationFrozenMethod;
    private final IInterface manager;
    private Method thawRotationMethod;

    public WindowManager(IInterface manager) {
        this.manager = manager;
    }

    private Method getGetRotationMethod() throws NoSuchMethodException {
        if (this.getRotationMethod == null) {
            Class<?> cls = this.manager.getClass();
            try {
                this.getRotationMethod = cls.getMethod("getDefaultDisplayRotation", new Class[0]);
            } catch (NoSuchMethodException e) {
                this.getRotationMethod = cls.getMethod("getRotation", new Class[0]);
            }
        }
        return this.getRotationMethod;
    }

    private Method getFreezeRotationMethod() throws NoSuchMethodException {
        if (this.freezeRotationMethod == null) {
            this.freezeRotationMethod = this.manager.getClass().getMethod("freezeRotation", Integer.TYPE);
        }
        return this.freezeRotationMethod;
    }

    private Method getIsRotationFrozenMethod() throws NoSuchMethodException {
        if (this.isRotationFrozenMethod == null) {
            this.isRotationFrozenMethod = this.manager.getClass().getMethod("isRotationFrozen", new Class[0]);
        }
        return this.isRotationFrozenMethod;
    }

    private Method getThawRotationMethod() throws NoSuchMethodException {
        if (this.thawRotationMethod == null) {
            this.thawRotationMethod = this.manager.getClass().getMethod("thawRotation", new Class[0]);
        }
        return this.thawRotationMethod;
    }

    public int getRotation() {
        try {
            Method method = getGetRotationMethod();
            return ((Integer) method.invoke(this.manager, new Object[0])).intValue();
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
            return 0;
        }
    }

    public void freezeRotation(int rotation) {
        try {
            Method method = getFreezeRotationMethod();
            method.invoke(this.manager, Integer.valueOf(rotation));
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
        }
    }

    public boolean isRotationFrozen() {
        try {
            Method method = getIsRotationFrozenMethod();
            return ((Boolean) method.invoke(this.manager, new Object[0])).booleanValue();
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
            return false;
        }
    }

    public void thawRotation() {
        try {
            Method method = getThawRotationMethod();
            method.invoke(this.manager, new Object[0]);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
        }
    }

    public void registerRotationWatcher(IRotationWatcher rotationWatcher, int displayId) {
        try {
            Class<?> cls = this.manager.getClass();
            try {
                cls.getMethod("watchRotation", IRotationWatcher.class, Integer.TYPE).invoke(this.manager, rotationWatcher, Integer.valueOf(displayId));
            } catch (NoSuchMethodException e) {
                cls.getMethod("watchRotation", IRotationWatcher.class).invoke(this.manager, rotationWatcher);
            }
        } catch (Exception e2) {
            throw new AssertionError(e2);
        }
    }
}